﻿//#define CheckOnline;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using GCELibrary.Current;
using System.Web;
namespace ZF.BLL.Page
{
    /// <summary>
    /// 会员页面，本页面必须登录之后才可以访问
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public class _MemberPage<T> : PageBase where T : IMember
    {
        //protected Gate.GateBase GateBase { get { return SiteInfo.CreateGate(); } }

        #region 用户
        //当前登录用户
        private T _currentUser = default(T);
        /// <summary>
        /// 当前登录的用户类型
        /// </summary>
        public virtual T CurrentUser
        {
            get
            {
                if (_currentUser == null)
                {
                    var user = this.SiteInfo.GetCurrentMember();//获取当前登录的用户
                    if (user is T && user != null)//判断用户类型
                        _currentUser = (T)user;
                    else
                        AlertTimeOut();//显示超时
                }
                return _currentUser;
            }
        }
        #endregion

        #region 重载
        protected override void OnInit(EventArgs e)
        {

            CheckSafe();//安全验证
            base.OnInit(e);
        }
        #endregion

        #region 安全
        /// <summary>
        /// 安全验证
        /// </summary>
        protected virtual void CheckSafe()
        {
            CheckLogin();//检查是否登录
            CheckUserType();//验证用户类型
            CheckRight();
        }
        /// <summary>
        /// 验证当前用户是否已经登录
        /// </summary>
        /// <returns></returns>
        protected virtual void CheckLogin()
        {
            if (this.CurrentUser == null)
            {
                AlertTimeOut();
            }
        }
        /// <summary>
        /// 显示登录超时或未登录信息
        /// </summary>
        protected virtual void AlertTimeOut()
        {


            var from=Server.UrlEncode(Request.Url.ToString());
            this.WarnFactory.CreateWarn(new PageWarnParameter()
            {

                DoPage = string.Format("{0}?from={1}", LoginPage, from),
                Icon = WarnIcon.Error,
                JscriptMode = true,
                AlertPage = "",
                Link = WarnLink.GoLogin,
                WarnCode = (int)WarnCode.LoginTimeOut
            }).Alert();



            ////显示登录超时信息
            //this.WarnFactory.CreateWarn(WarnCode.LoginTimeOut).Alert();
        }
 
        /// <summary>
        /// 验证用户类型
        /// </summary>
        protected virtual void CheckUserType()
        {
            if (!(CurrentUser is T))
            {
                AlertOverRight();
            }
        }
        /// <summary>
        /// 显示越权信息
        /// </summary>
        protected virtual void AlertOverRight()
        {
            throw WarnFactory.CreateWarn(GCELibrary.Current.WarnCode.OverRight);
        }

        protected virtual void CheckRight()
        {

        }
        #endregion

        protected virtual string LoginPage { get { return "/login.aspx"; } }
    }

    public class GMPage : _MemberPage<GeneralMember>
    {
        /// <summary>
        /// 报告生成器
        /// </summary>
        public override WarnFactory WarnFactory { get { return new WarnFactory("/Person/warn.aspx"); } }


 
    }
    public class MMPage : _MemberPage<ManageMember>
    {
        private ModuleType _Module = ModuleType.None;
        public virtual ModuleType Module { get { return _Module; } protected set { _Module = value; } }

        /// <summary>
        /// 报告生成器
        /// </summary>
        public override WarnFactory WarnFactory { get { return new WarnFactory(string.Format("{0}/warn.aspx", AppKey.SiteKey.ManageDirectory)); } }

        protected override string LoginPage { get { return string.Format("{0}/login.aspx", AppKey.SiteKey.ManageDirectory); } }


        protected override void CheckRight()
        {
            base.CheckRight();
            if (Module != ModuleType.None)
            {
                var ms = this.Module.ToString();
                var right = CurrentUser.RightModules.Contains(ms);
              
                //using (var dc = SiteInfo.CreateDBDataContext())
                //{
                //    right=dc.US_Member.FirstOrDefault(p => p.MemberId == CurrentUser.MemberId).Right.EmptyString("").Split(
                //        new string[] { "," }, StringSplitOptions.RemoveEmptyEntries).Contains(ms);
                //}

                
                if (!right)
                {
                    WarnFactory.CreateWarn("当前帐号无权限访问该页面", WarnIcon.Error, WarnLink.GoBack).Alert();

                }
            }
        }
    }

    public class LMPage : _MemberPage<IMember>
    {
        /// <summary>
        /// 报告生成器
        /// </summary>
        public override WarnFactory WarnFactory
        {
            get
            {
                var member = SiteInfo.GetCurrentMember();
                if (member == null)
                    return new WarnFactory("/warn.aspx");
                else if (member.UserType == UserType.Member)
                    return new WarnFactory("/Person/warn.aspx");
                else if (member.UserType == UserType.Admin)
                    return new WarnFactory(string.Format("{0}/warn.aspx", AppKey.SiteKey.ManageDirectory));
                else
                    throw new Exception("LMPage.WarnFactory");

            }
        }

    }
}
